package com.log.service.Impl;

import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogDepotMapper;
import com.log.pojo.LogDepot;
import com.log.pojo.LogGoodsOutIn;
import com.log.service.LogDepotService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author wjy
 * @create 2023-10-2023/10/21 20:21
 */
@Service
public class LogDepotServiceImpl implements LogDepotService{
    @Autowired
    private LogDepotMapper logDepotMapper;
    @Override
    public ResultVo addLogDepot(@RequestBody LogDepot logDepot) {
        //1.查询名字是否重复
        String logDepotName = logDepot.getLogDepotName();
        Example example = new Example(LogDepot.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logDepotName",logDepotName);
        List<LogDepot> logDepots = logDepotMapper.selectByExample(example);
        if(logDepots.size()>0)
        {
            return new ResultVo(ResStatus.NO,"添加失败,已存在改名字",null);
        }
        int i = logDepotMapper.insert(logDepot);
        if(i>0)
        {
            return new ResultVo(ResStatus.OK,"success",null);
        }
        else{
            return new ResultVo(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo updateLogDepot(@RequestBody LogDepot logDepot) {
        int i = logDepotMapper.updateByPrimaryKey(logDepot);
        if(i>0)
        {
            return new ResultVo(ResStatus.OK,"success",null);
        }
        else{
            return new ResultVo(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo deleteLogDepot(int id) {
        int i = logDepotMapper.deleteByPrimaryKey(id);
        if(i>0)
        {
            return new ResultVo(ResStatus.OK,"success",null);
        }
        else{
            return new ResultVo(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo getLogDepotByPage(int pageNum, int limit) {
        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogDepot> list = logDepotMapper.selectLogDepotByPage(start, limit);
        //3.查询总数
        Example example = new Example(LogDepot.class);
        Example.Criteria criteria = example.createCriteria();
        int count = logDepotMapper.selectCountByExample(example);
        //3.计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"success",new PageHelper<LogDepot>(count,pageCount,list));
    }

    @Override
    public ResultVo getLogDepotById(int id) {
        LogDepot logDepot = logDepotMapper.selectByPrimaryKey(id);
        return new ResultVo(ResStatus.OK,"success",logDepot);
    }

    @Override
    public ResultVo getAll() {
        return new ResultVo(ResStatus.OK,"success",logDepotMapper.selectAll());
    }
}
